var key = 0;
function s (select) {
    return document.querySelector(select);
}
//登陆改变图标
function changeIcon(){
    var token = selectByKey("like_token");
    if(token){
        var imgs = document.getElementsByTagName("a")[1];
        imgs.style.backgroundImage = "url('../../statics/images/index/Group.png')";
        imgs.style.backgroundSize = "23px 23px";
        imgs.style.backgroundPosition = "-1px 0px";
        imgs.style.backgroundRepeat = "no-repeat";
    }
}
//改变头部标题
function changetitle (cid) {
    var title;
    switch (cid) {
        case 3:
            title = '喜欢资讯';
            break;
        case 4:
            title = '婚礼风俗';
            break;
        case 5:
            title = '常见问题';
            break;
        default:
            title = '时尚资讯';
            break;
    }
    s('header label').innerHTML = title;
}
//获取发布时间差
function getTimeNum (time) {
    var date = {}
    var preTime = time.replace(/\-/g,'/');
    var preTimeTurn = Date.parse(new Date(preTime));
    var nowDate = new Date().getTime();
    var timeCha = (nowDate - preTimeTurn)/1000;
    if(timeCha / 3600 < 1){
        date.num = parseInt(timeCha / 60);
        date.timeType = '分钟';
        return date;
    }else if(timeCha / 86400 < 1){
        date.num = parseInt(timeCha / 3600);
        date.timeType = '小时';
        return date;
    }else if(timeCha / 31104000 < 1){
        date.num = parseInt(timeCha / 86400);
        date.timeType = '天';
        return date;
    }else {
        date.num = parseInt(timeCha / 31104000);
        date.timeType = '年';
        return date;
    }
}
//ajax模板
function ajaxHtml (data,htmlMode,htmlNode) {
    var html = '';
    for(var i = 0; i < data.length; i++){
        var htmlTemp = htmlMode.innerHTML;
        var timeObj = getTimeNum(data[i].created_at);
        htmlTemp = htmlTemp.replace(/\{\{time_num\}\}/,timeObj.num);
        htmlTemp = htmlTemp.replace(/\{\{hour_or_day\}\}/,timeObj.timeType);
        htmlTemp = htmlTemp.replace(/\{\{cid\}\}/,data[i].cid);
        htmlTemp = htmlTemp.replace(/\{\{id\}\}/,data[i].id);
        htmlTemp = htmlTemp.replace(/\{\{src\}\}/,data[i].thumb_pic);
        htmlTemp = htmlTemp.replace(/\{\{description\}\}/,data[i].title);
        html += htmlTemp;
    }
    htmlNode.innerHTML += html;
}
//ajax模板END
function search (cid,pageNum) {
    $.ajax({
        url:pathName + "/api/v1/wedding",
        type:"get",
        data:{
            "cid": cid,
            "type": 1,
            "page": pageNum
        },
        success:function(value){
            console.log(value);
            if(value.current_page < value.last_page){
                k = 0;
            }
            s('.mask').style.display = 'none';
            ajaxHtml (value.data,s('#listMode'),s('#fmain'));
            toDetail ();
            shoucan();
        }
    })
}
function shoucan() {
    var scs = document.querySelectorAll('.sc'), i = 0,path='../../statics/images/school';
    for (; i < scs.length; i++) {
        Tap(scs[i], function (e, dom) {
            var domN;
            if(dom.children[0] != undefined){
                domN = dom.children[0]
            }else{
                domN = dom;
            }
            var pngname = domN.src.split('/')[domN.src.split('/').length - 1];
            if (pngname == 'wsc.png') {
                domN.src = path + '/sc.png';

            } else {
                domN.src = path + '/wsc.png';
            }
        })
    }
};
function toDetail () {
    var topics = document.querySelectorAll('.content');
    for(var i = 0; i < topics.length; i++){
        Tap(topics[i],function(e,dom){
            var cid = dom.parentNode.getAttribute('data-cid');
            var id = dom.parentNode.getAttribute('data-id');
            window.location.href = 'newcontent.html?cid='+ cid + '&id=' + id;
        });
    }
}
//收藏模块END
window.onload = function () {
    totop('.TOTOP');
    changeIcon();
    var cid = parseInt(location.search.split('=')[1]);
    changetitle (cid);
    var enter = document.querySelector('header a:last-child');
    Tap(enter,function(){
        if(selectByKey("like_token")){
            window.location.href = '../personal/individual.html';
        }else{
            window.location.href = '../personal/enter.html';
        }
    });
    var pageNum = 1;
    //页面加载完毕第一次ajax
    search(cid,pageNum);
    //END
    window.onscroll = function () {
        var t = document.documentElement.scrollTop || document.body.scrollTop;
        //滚动过的高度(屏幕顶到最上面··不包括现在的屏幕高度)
        var y = document.documentElement ? document.documentElement.scrollHeight : document.body.scrollHeight;
        //浏览器滚动条总高
        var w = document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight;
        //屏幕高度
        if (t > 100) {
            document.querySelector('.TOTOP').style.opacity = 1;
        } else {
            document.querySelector('.TOTOP').style.opacity = 0;
        }
        if (t + w > y - 200) {
            if (key == 0) {
                key = 1;
                pageNum++;
                s('.mask').style.display = 'block';
                search(cid,pageNum);
            }
        }
    };
};